<?php
class blockContent_Mapper extends content_Mapper{
    private $selectStmt;
    private $updateStmt;
    private $insertStmt;
    private $findByNameStmt;

    function  __construct() {
        parent::__construct();
        $this->selectStmt = self::$PDO->prepare(
            "SELECT * FROM blocks WHERE moduleId = ?");
        $this->updateStmt = self::$PDO->prepare(
            'UPDATE blocks SET
                moduleTitle =?,                
                moduleContent =?,
                moduleName=?,
                moduleStyle =?,
                moduleTemplate=?
             WHERE  moduleId=?');
        $this->insertStmt = self::$PDO->prepare(
            'INSERT into blocks SET
                moduleTitle =?,                
                moduleContent =?,
                moduleName=?,
                moduleStyle = ?,
                moduleTemplate=?');
       $this->findByNameStmt = self::$PDO->prepare(
            'SELECT * FROM blocks WHERE moduleName LIKE ? '
      );


    }

    public function findByName( $var ) {
        $this->findByNameStmt->execute(array("%$var"));
        $array = $this->findByNameStmt->fetch();
        $this->findByNameStmt->closeCursor( );
        if ( ! is_array( $array ) ) { return null; }
        $object = $this->createObject( $array );
        return $object;
    }

    

    function update($obj){
        $values = array(
            $obj->getTitle(),
            $obj->getBody(),
            $obj->getName(),
            $obj->getStyle(),
            $obj->getTemplate(),
            $obj->getId()
            

        );        
        $this->updateStmt->execute( $values );
        return $obj;
    }
    
    protected function doInsert($obj){
        $values = array(
            $obj->getTitle(),
            $obj->getBody(),
            $obj->getName(),
            $obj->getStyle(),
             $obj->getTemplate()
        );
        $this->insertStmt->execute( $values );
        $obj->setId(self::$PDO->lastInsertId());
        return $obj;
    }

    protected  function doCreateObject( array $array ){
        $obj = new blockContentObject();
        $obj->setId($array['moduleId']);
        $obj->setActiv(1);
        $obj->setAddData('00-00-000');
        $obj->setBody($array['moduleContent']);                
        $obj->setName($array['moduleName']);
        $obj->setStyle($array['moduleStyle']);
        $obj->setTitle($array['moduleTitle']);
        $obj->setTemplate($array['moduleTemplate']);
        return $obj;
    }
   /* public function save($object){
        if($object->getId() < 1){
            return $this->doInsert($object);
        } else {
            return $this->update($object);
        }
    }*/
    protected  function targetClass(){
        return 'blocks';
    }
    protected  function selectStmt(){
        return $this->selectStmt;

    }
}

class blockContent_Mapper_Collection extends content_Mapper_Collection{
    function targetClass(){
        return 'blocks';
    }
}
?>
